package com.itdianzi.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.controller.util.FileUploadUtil;
import com.itdianzi.domain.cargo.ContractProduct;
import com.itdianzi.domain.cargo.ContractProductExample;
import com.itdianzi.domain.cargo.Factory;
import com.itdianzi.domain.cargo.FactoryExample;
import com.itdianzi.service.cargo.ContractProductService;
import com.itdianzi.service.cargo.ContractService;
import com.itdianzi.service.cargo.FactoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/cargo/contractProduct")
public class ContractProductController extends BaseController {
    @Reference
    private ContractProductService contractProductService;
    @Reference
    private ContractService contractService;
    @Reference
    private FactoryService factoryService;
    @RequestMapping("/list")
    public String list(String contractId, @RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue = "10") Integer size) {
        //查询购销合同下的货物数据 contractId 合同ID 需要分页
        ContractProductExample contractProductExample = new ContractProductExample();
        ContractProductExample.Criteria criteria = contractProductExample.createCriteria();
        //根据合同的ID查询货物的数据
        criteria.andContractIdEqualTo(contractId);
        PageInfo pageInfo = contractProductService.findAll(contractProductExample, page, size);

        request.setAttribute("page", pageInfo);

        //2.查询添加列表中 生产厂家的 下拉列表数据 但是 注意:只能查询生产货物的厂家 不需要分页
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeEqualTo("货物");

        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        //3.存入购销合同id  页面的保存功能 和其他功能需要使用
        request.setAttribute("contractId",contractId);

        return "cargo/product/product-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "cargo/contract/contract-add";
    }

    @Autowired
    private FileUploadUtil fileUploadUtil;

    /**
     * 保存或修改
     * 如果上传了图片
     * 注意: 需要有一个对象来接收
     *
     * 数据库中只存储图片的路径
     * http://px1fec9og.bkt.clouddn.com/FlCzk5dR6JlwX49gQXuzdrEyKYu9\
     * 后台需要先接收图片对象数据
     *
     * spring mvc 提供MultipartFile 专门用来接收文件上传的对象
     * @return
     */
    @RequestMapping("/edit")
    public String edit(ContractProduct contractProduct, String contractId, MultipartFile productPhoto) throws Exception {
        if (StringUtils.isBlank(contractProduct.getId())) {
            if (productPhoto != null) { //进行文件上传  别人已经选了图片对象
                String imagePath = fileUploadUtil.upload(productPhoto);
                //添加成功后返回一个互联网图片的路径
                contractProduct.setProductImage(imagePath);
            }

            contractProductService.save(contractProduct);
        } else {
            contractProductService.update(contractProduct);

        }
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractId;
    }



    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        //查询货物的数据
        ContractProduct contractProduct = contractProductService.findById(id);
        request.setAttribute("contractProduct",contractProduct);

        //查询添加列表中生产厂家的下拉列表数据，但是注意：只能查询生产货物的厂家 不需要分页
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        System.out.println(factoryList);
        request.setAttribute("factoryList", factoryList);

        return "cargo/product/product-update";
    }

    @RequestMapping("/delete")
    public String delete(String id, String contractId) {

        contractProductService.delete(id);

        return  "redirect:/cargo/contractProduct/list.do?contractId="+contractId;
    }





}
